//
//  QDDoctorApplyController.m
//  simulateQuickDoctor
//
//  Created by 姚华杰 on 2017/3/23.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDDoctorApplyController.h"
#import "QDDateDoctorController.h"
#import "QDDoctorAttentionMOdel.h"
#import "QDDoctorCell.h"
#import "QDDAModel.h"
#import <YYModel.h>
#import "QDNetworkTool.h"
#import <MJRefresh.h>

@interface QDDoctorApplyController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray<QDDAModel *> *dataList;

@property(nonatomic,strong)MJRefreshNormalHeader *headRefesh;

@property(nonatomic,strong)MJRefreshAutoNormalFooter *footRefesh;


//SB中的tableView视图
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation QDDoctorApplyController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _dataList = [NSArray array];
    
    [self setupUI];
    
    [self loadData];
    
    [self setupRefresh];

}

- (void)setupUI {
    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"QDDoctorCell" bundle:nil] forCellReuseIdentifier:@"YSCell"];
    
    //设置数据源
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    //设置预估行高
    _tableView.estimatedRowHeight = 200;
    //设置真实行高
    _tableView.rowHeight = UITableViewAutomaticDimension;
    
    
    NSUInteger naviType = [self.parameters[@"naviType"] integerValue];
    if(naviType == 0){
        self.navigationItem.title = @"名医申请";
    }else{
        self.navigationItem.title = @"预约列表";
    }

}

-(void)loadData {

    
    NSDictionary *parameters = @{@"ci1_id":@"1",@"ci2_id":@"3",@"ci3_id":@"3",@"diagnosis_type":@"0",@"page_size":@"15",@"is_confirmed":@"1",@"user_id":@"1000089", @"page":@"1",@"has_diagnosis":@"2"};
    
    NSString* urlString = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    
    
    QDNetworkTool* tool = [QDNetworkTool sharedTool];
    
    
    tool.requestSerializer = [AFJSONRequestSerializer serializer];
    [tool requestWithHTTPMethod:@"POST" URLString:urlString parameters:parameters CallBack:^(id responseObj) {
        
        if(responseObj != nil){
            
            NSString* result = responseObj[@"msg"]; //FAIL
            
            if([result isEqualToString:@"OK"]){
                
                NSDictionary* arrDict = responseObj[@"data"];
                
                _dataList = [NSArray yy_modelArrayWithClass:[QDDAModel class] json:arrDict];
                
                [self.tableView reloadData];
            
                NSLog(@"%ld",_dataList.count);
                
                [_headRefesh endRefreshing];
                [_footRefesh endRefreshing];
                
            }else{
                NSLog(@"请求参数错误");
            }
            
        }else{
            NSLog(@"网络请求错误");
        }
    }];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QDDoctorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YSCell" forIndexPath:indexPath];
    
    QDDAModel *model = _dataList[indexPath.row];
    
    cell.model = model;
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSUInteger naviType = [self.parameters[@"naviType"] integerValue];
    
    if(naviType == 1){
        
        
        QDDateDoctorController*  vc = [[QDDateDoctorController alloc]init];
        
        QDDoctorCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        
        vc.parameters = @{@"entrance":@"doctorApply"};
        
        vc.model = cell.model;
        
//        NSLog(@"%@",cell.model);
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }
    
}


-(void)setupRefresh{
    
    //上拉刷新
    _headRefesh = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_header = _headRefesh;
    
    //下拉刷新
    _footRefesh = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = _footRefesh;
}

@end
